package com.example.factory.api.dto.assembler;

import com.example.factory.api.dto.DtoLevel1Dimension;
import com.example.factory.api.dto.DtoLevel2Dimension;
import com.example.factory.api.dto.form.DtoLevel1DimensionForm;
import com.example.factory.api.dto.form.DtoLevel2DimensionForm;
import com.example.factory.core.domain.Level1Dimension;
import com.example.factory.core.domain.Level2Dimension;
import com.example.factory.core.repository.Level1DimensionRepository;
import com.example.factory.core.repository.Level2DimensionRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lei Tao
 */
@Component
public class GradingRuleAssembler {
    private final Level1DimensionRepository level1DimensionRepository;
    private final Level2DimensionRepository level2DimensionRepository;

    public GradingRuleAssembler(Level1DimensionRepository level1DimensionRepository, Level2DimensionRepository level2DimensionRepository) {
        this.level1DimensionRepository = level1DimensionRepository;
        this.level2DimensionRepository = level2DimensionRepository;
    }

    public List<DtoLevel1Dimension> toDtoLevel1Dimensions(List<Level1Dimension> level1Dimensions) {
        return level1Dimensions.stream().map(this::toDtoLevel1Dimension).collect(Collectors.toList());
    }

    public DtoLevel1Dimension toDtoLevel1Dimension(Level1Dimension level1Dimension) {
        DtoLevel1Dimension dtoLevel1Dimension = new DtoLevel1Dimension();
        BeanUtils.copyProperties(level1Dimension, dtoLevel1Dimension);
        return dtoLevel1Dimension;
    }

    public Level1Dimension toLevel1Dimension(DtoLevel1DimensionForm dtoLevel1DimensionForm) {
        Level1Dimension level1Dimension;
        if (!StringUtils.isEmpty(dtoLevel1DimensionForm.getGuid())) {
            level1Dimension = level1DimensionRepository.findByGuidAndArchivedFalse(dtoLevel1DimensionForm.getGuid());
        } else {
            level1Dimension = new Level1Dimension();
        }
        BeanUtils.copyProperties(dtoLevel1DimensionForm, level1Dimension);
        return level1Dimension;
    }

    public List<DtoLevel2Dimension> toLevel2Dimensions(List<Level2Dimension> level2Dimensions) {
        return level2Dimensions.stream().map(this::toDtoLevel2Dimension).collect(Collectors.toList());
    }

    public DtoLevel2Dimension toDtoLevel2Dimension(Level2Dimension level2Dimension) {
        DtoLevel2Dimension dtoLevel2Dimension = new DtoLevel2Dimension();
        BeanUtils.copyProperties(level2Dimension, dtoLevel2Dimension);
        Level1Dimension level1Dimension = level2Dimension.getLevel1Dimension();
        dtoLevel2Dimension.setLevel1Name(level1Dimension.getName());
        dtoLevel2Dimension.setLevel1Guid(level1Dimension.getGuid());
        return dtoLevel2Dimension;
    }

    public Level2Dimension toLevel2Dimension(DtoLevel2DimensionForm dtoLevel2DimensionForm) {
        Level2Dimension level2Dimension;
        if (!StringUtils.isEmpty(dtoLevel2DimensionForm.getGuid())){
            level2Dimension = level2DimensionRepository.findByGuidAndArchivedFalse(dtoLevel2DimensionForm.getGuid());
        }else {
            level2Dimension = new Level2Dimension();
        }
        BeanUtils.copyProperties(dtoLevel2DimensionForm,level2Dimension);
        Level1Dimension level1Dimension = level1DimensionRepository.findByGuidAndArchivedFalse(dtoLevel2DimensionForm.getLevel1Guid());
        level2Dimension.setLevel1Dimension(level1Dimension);

        return level2Dimension;
    }
}
